package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CatalogDao;
import com.itheima.domain.Catalog;
import com.itheima.service.CatalogService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class CatalogServiceImpl implements CatalogService {
    @Override
    public PageInfo<Catalog> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Catalog> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Catalog> list=catalogDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Catalog catalog) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);
            String id = UUID.randomUUID().toString();
            catalog.setId(id);
            catalogDao.save(catalog);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Catalog findById(String id) {
        SqlSession sqlSession = null;
        Catalog catalog= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);
            catalog = catalogDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return catalog;
    }

    @Override
    public void update(Catalog catalog) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);

            catalogDao.update(catalog);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);
            Stream.of(ids).forEach(id->catalogDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Catalog> findAll() {
        SqlSession sqlSession = null;
        List<Catalog> list = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CatalogDao catalogDao = MybatisUtil.getMapper(sqlSession, CatalogDao.class);
            list=catalogDao.findAll();

        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }
}
